<template>
	<view :style="objColor">
		<!-- mine/pages/myCarList/myCarList.wxml -->
		<template v-if="advertisingSwitch">
			<adModule :adIdList="adIdList"/>
		</template>

		<view v-if="carList.length > 0" style="padding-bottom: 220rpx">
			<view class="carItem" v-for="(item, index) in carList" :key="index" @tap="selectCar"
				:data-plateno="item.plateNo">
				<view class="carInfo">
					<image :src="item.url" class="img"></image>
					<view>
						<view class="carName">
							<view class="name1">{{ item.brandName || '' }}{{ item.carTypeName ? item.carTypeName : '' }}
							</view>
							<view :class="item.vehicleType == 'DianDong' ? 'text1' : 'text2'">
								{{ item.vehicleType == 'DianDong' ? '纯电动' : '混合动力' }}</view>
						</view>
						<view class="chePai">{{ item.plateNo }}</view>
					</view>
				</view>

				<view class="operation">
					<view class="isDefault"
						@tap="parseEventDynamicCode($event, item.defaultVehicle != 0 ? 'setDefault' : '')"
						:data-id="item.id">
						<image
							:src="item.defaultVehicle == 0 ? sellerNo=='jw'?'https://oos-cn.ctyunapi.cn/front-end/static/mine/images/checkbox_sel-blue.png':'https://oos-cn.ctyunapi.cn/front-end/static/mine/images/checkRefund_sel.png' : 'https://oos-cn.ctyunapi.cn/front-end/static/mine/images/checkRefund.png'"
							:data-id="item.id" class="img1"></image>
						设为默认
					</view>
					<view style="display: flex;color: #606266;font-size: 30rpx;align-items: center;"
						@tap="changeFlag(item)">
						更多<image
							:src="item.isFlag?'https://oos-cn.ctyunapi.cn/front-end/static/static/More-s.png':'https://oos-cn.ctyunapi.cn/front-end/static/static/More-n.png'"
							style="width: 33rpx;height: 34rpx;margin-left: 15rpx;"></image>
					</view>
					<view class="dialog" v-if="item.isFlag">
						<view class="del" :data-id="item.id" @tap="delCar">删除</view>
						<view class="edit" @tap="editCar" :data-carinfo="item">编辑</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="carList.length <= 0&&loading">
			<Upgrading text="暂无爱车"></Upgrading>
		</view>
		<view class="fot_btn">
			<!-- <view class="btn" bindtap="{{deductMode=='LeaguerAccount'&&carList.length==1?'showTip':'addCar'}}"> -->
			<view class="btn" @tap="addCar">
				<!-- <image src="https://oos-cn.ctyunapi.cn/front-end/static/mine/images/Add.png" class="img"></image> -->
				<uni-icons class="add_icon" type="plus" size="20" :color="'#ffffff'"></uni-icons>
				添加爱车，享专属服务
			</view>
		</view>
	</view>
</template>

<script>
	'use strict';
	import Upgrading from '../../../components/upgrading/upgrading';
	import adModule from '@/components/adModule/adModule';
	// exports.__esModule = true; // mine/pages/myCarList/myCarList.ts
	var app = getApp();
	var https_1 = require('./../../../api/https.js');
	export default {
		components: {
			Upgrading,
			adModule
		},
		data() {
			return {
				sellerNo: app.globalData.sellerNo,
				loading: false,
				deductMode: '',
				carList: [],
				type: '',
				adIdList: ['WX_APPLET_ElPP']
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(option) {
			if (option.type === 'select') {
				this.type = 'select'
			}
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			this.deductMode = uni.getStorageSync('deductMode')
			this.getVehicleList();
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			//我的车辆列表
			getVehicleList: function() {
				var that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				that.loading = false
				https_1.getVehicleList().then(function(res) {
					if (res.code == 0) {
						that.carList = res.data
						that.carList = that.carList.map(item => {
							item.isFlag = false
							return item
						})
						uni.hideLoading();
						that.loading = true
					}
				});
			},
			changeFlag(e) {
				this.carList = this.carList.map(item => {
					if (item.id === e.id) {
						item.isFlag = !e.isFlag
					}
					return item
				})

			},
			//新增车辆
			addCar: function() {
				uni.navigateTo({
					url: '../addCar/addCar'
				});
			},

			showTip: function() {
				uni.showModal({
					title: '信息提示',
					content: '子成员只能添加一辆车辆！',
					showCancel: false
				});
			},
			selectCar(e) {
				var plateNo = e.currentTarget.dataset.plateno;
				getApp().globalData.plateNo = plateNo
				if (this.type === 'select') {
					uni.navigateBack({
						delta: 1
					});
				}
			},
			// 编辑车辆
			editCar: function(e) {
				console.log("打印", e)
				// var data = e.target.dataset.carinfo;
				var data = e.currentTarget.dataset.carinfo;
				// delete data.url; // console.log("打印data",data)

				uni.navigateTo({
					url: '../addCar/addCar?type=edit',
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						acceptDataFromOpenedPage: function(data) {
							// console.log(data)
						},
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						// console.log(data)
						res.eventChannel.emit('acceptDataFromOpenerPage', data)
					}
				});
			},

			//设置默认车辆
			setDefault: function(e) {
				var that = this;
				https_1
					.updateDefaultVehicle({
						vehicleId: e.target.dataset.id
					})
					.then(function(res) {
						if (res.code == 0) {
							that.getVehicleList();
						}
					});
			},

			//会员车辆-删除
			delCar: function(e) {
				var that = this;
				uni.showModal({
					title: '',
					content: '确定删除此车信息吗',
					confirmColor: that.objColor2.textColor,
					success: function(res) {
						if (res.confirm) {
							https_1
								.delVehicleInfo({
									id: e.target.dataset.id
								})
								.then(function(res) {
									if (res.code == 0) {
										that.getVehicleList();
									}
								});
						} else if (res.cancel) {
							//   console.log('用户点击取消')
						}
					}
				});
			}
		}
	};
</script>
<style lang="less">
	@import url('./myCarList.less');
</style>